﻿using System;

namespace Shp.Web.UI.WebControls.Iconized.Designers
{
   /// <summary>
   /// IconizedButton Designer class.
   /// </summary>
   public class IconizedButtonDesigner : IconizedButtonDesignerBase
   {
      #region Fields
      #endregion Fields

      #region Propertiess
      /// <summary>
      /// Gets the designed control.
      /// </summary>
      /// <value>The designed control.</value>
      protected override IIconizedButton DesignedControl
      {
         get { return (this.Component as IIconizedButton); }
      }

      /// <summary>
      /// Gets the design time template.
      /// {0}: mouse-out style
      /// {1}: round corners class
      /// {2}: icon position class
      /// {3}: button skin class
      /// {4}: disabled class
      /// {5}: disabled attribute
      /// {6}: the icon type
      /// {7}: the button text
      /// </summary>
      /// <value>The design time template.</value>
      private string DesignTimeTemplate
      {         
         get { return "<a href=\"javascript:void(0);\" class=\"shp-btn{0}{1}{2}{3}{4}\"{5}> <span class=\"shp-ico{6}\"></span>{7}</a>"; }
      }

      /// <summary>
      /// Gets the control's design time markup.
      /// </summary>
      /// <value>The design time markup.</value>
      protected string DesignTimeMarkup
      {
         get
         {
            //WebControl btn = (this.DesignedControl as WebControl);

            // display mouse out style
            string mouseOutClass = (!this.DesignedControl.DisplayMouseOutStyle) ? " ni" : string.Empty;

            // round corners class
            string roundClass = (this.DesignedControl.RoundCorners) ? " round" : string.Empty;

            string iconPostion = string.Empty;

            // icon position class
            if (this.DesignedControl.IconType != IconType.None)
            {
               switch (this.DesignedControl.IconPosition)
               {
                  case IconPositionType.None:
                     iconPostion = (this.DesignedControl.Text.Trim() != string.Empty) ? " shp-btn-ico-lt" : " shp-btn-ico-s";
                     break;
                  case IconPositionType.Left:
                     iconPostion += " shp-btn-ico-lt";
                     break;
                  case IconPositionType.Right:
                     iconPostion += " shp-btn-ico-rt";
                     break;
                  case IconPositionType.Solo:
                     iconPostion += " shp-btn-ico-s";
                     // solo icon does not allow text, so remove the text.
                     this.DesignedControl.Text = string.Empty;
                     break;
                  case IconPositionType.SmallSolo:
                     iconPostion += " shp-btn-ico-ss";
                     // solo icon does not allow text, so remove the text.
                     this.DesignedControl.Text = string.Empty;
                     break;
               }
            }

            // button skin class
            string skinClass = string.Format(" {0}", (this.DesignedControl.ButtonSkin != ButtonSkinType.Custom) ?
               this.DesignedControl.ButtonSkin.ToString("G").ToLower() :
               this.DesignedControl.CustomCssClass);

            // disabled class
            string disabledClass = !(this.DesignedControl.Enabled) ? " disabled" : string.Empty;

            // disabled attribute
            string disabled = !(this.DesignedControl.Enabled) ? " disabled=\"disabled\"" : string.Empty;

            // icon type class
            // performance decision: only render a single icon, because, design time rendering with all available icons freezes the VS designer.
            string iconType =
               (this.DesignedControl.IconType == IconType.None) ? string.Empty :
               string.Format(" {0}", ((this.DesignedControl.IconType == IconType.Custom) ? this.DesignedControl.CustomIconType : IconType.Image.ToString("G")));

            return string.Format(this.DesignTimeTemplate,
               mouseOutClass,
               roundClass,
               iconPostion,
               skinClass,
               disabledClass,
               disabled,
               iconType,
               this.DesignedControl.Text);
         }
      }
      #endregion Properties

      #region Methods
      /// <summary>
      /// Retrieves the HTML markup that is used to represent the control at design time.
      /// </summary>
      /// <returns>
      /// The HTML markup used to represent the control at design time.
      /// </returns>
      public override string GetDesignTimeHtml()
      {
         return this.BuildDesignTimeHtml();
      }

      /// <summary>
      /// Builds the design time HTML - including CSS Web Resources.
      /// </summary>
      /// <returns></returns>
      protected override string BuildDesignTimeHtml()
      {
         //if (this.DesignedControl is IconizedButton)
         return string.Format("{0}{1}{2}", base.BuildDesignTimeHtml(), Environment.NewLine, this.DesignTimeMarkup);
         //else
         //{
         //   // IconizedHyperLink is throwing a design time exception, so for now just return the design time markup for the control.
         //   return this.DesignTimeMarkup;
         //}
      }
      #endregion Methods

      #region Ctors
      #endregion Ctors
   }
}
